import { Component, Input, ViewChild } from '@angular/core';
import { environment } from '@env/environment';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { FormBuilder, FormGroup } from '@angular/forms';
import { AuthDictService } from '../../../../system/dict/service/dict.service';
import { DragModalService } from '../../../../../shared/utils/drag-modal.service';
import { ShipStatService } from '../../ship-stat.service';
import { NzDrawerService } from 'ng-zorro-antd/drawer';
import { EditAllPieceTonComponent } from '../../edit-all-piece-ton/edit-all-piece-ton.component';
import { BillDetailComponent } from '../../bill-detail/bill-detail.component';

@Component({
	selector: 'app-import-ship-stat-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less']
})
export class ImportShipStatListComponent {
	@Input()
	shipInfo: any;

	pageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	pageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};

	@ViewChild('st', { static: false }) private st!: STComponent;
	columns: STColumn[] = [
		{
			title: '序号',
			width: 50,
			index: 'index',
			type: 'no',
			noIndex: (item, col, index) => index + 1 + (this.st.pi - 1) * this.st.ps,
			fixed: 'left',
			className: 'tc'
		},
		{
			title: '单号',
			fixed: 'left',
			index: 'billNo',
			render: 'billNo',
			width: 150
		},
		{
			title: '货主',
			fixed: 'left',
			index: 'cargoOwnerName',
			width: 100
		},
		{
			title: '货名',
			fixed: 'left',
			index: 'cargoTypeName',
			width: 80
		},
		{
			title: '包装',
			index: 'billPackageTypeName',
			width: 80
		},
		{
			title: '卸齐',
			index: "isUnloadClear",
			render: 'isUnloadClear',
			width: 60
		},
		{
			title: '提清',
			index: 'isDeliveryClear',
			render: 'isDeliveryClear',
			width: 60
		},
		{
			title: '总件吨',
			index: 'allPieceTonInfo',
			width: 120
		},
		{
			title: '卸船',
			index: 'inPieceTonInfo',
			width: 120
		},
		{
			title: '提货',
			index: 'outPieceTonInfo',
			width: 120
		},
		{
			title: '直提',
			index: 'directOutPieceTonInfo',
			width: 120
		},
		{
			title: '库存',
			index: 'storagePieceTonInfo',
			width: 120
		},
		{
			title: '转入',
			index: 'transferInPieceTonInfo',
			width: 120
		},
		{
			title: '转出',
			index: 'transferOutPieceTonInfo',
			width: 120
		},
		{
			title: '全部货场',
			index: 'positions',
			width: 100
		},
		{
			title: '状态',
			fixed: 'right',
			index: 'isFinish',
			render: 'isFinish',
			width: 80
		},
		{
			title: '操作',
			className: 'tc',
			width: 100,
			fixed: 'right',
			render: 'manage'
		}
	];
	dataList = [];
	totalData:any;

	searchForm: FormGroup;
	shippingOrderStatusList = [];
	loadClearList = environment.dict.loadClear;
	ynList = environment.dict.yesOrNoStr;
	isFinishList = environment.dict.isFinish;
	shipList = [];
	billList = [];

	constructor(
		private fb: FormBuilder,
		private dictService: AuthDictService,
		private modalService: DragModalService,
		private shipStatService: ShipStatService,
		private drawerService: NzDrawerService
	) {
		this.dictService.dictionary(environment.dict.SHIPPING_ORDER_STATUS, 1).subscribe(res => {
			this.shippingOrderStatusList = res;
		});
		this.searchForm = this.fb.group({
			billNo: null,
			isUnloadClear: null,
			isDeliveryClear: null,
			isFinish: null,
		});
	}

	search(): any {
		this.loadData(this.shipInfo.transportShipId);
	}

	reset() {
		this.searchForm.reset();
		this.loadData(this.shipInfo.transportShipId);
	}

	resetParams(): void {
		this.pageInfo.current = 1;
		for (let key of Object.keys(this.shipInfo)) {
			this.shipInfo[key] = "";
		}
	}

	loadData(shipId: string): any {
		const params = {
			transportShipId: shipId,
			...this.searchForm.value,
			size: this.pageInfo.size,
			current: this.pageInfo.current
		};

		this.pageInfo.loading = true;
		this.shipStatService.shipImpBillStatPage(params).subscribe((res: any) => {
			this.dataList = res.records;
			this.pageInfo.total = res.total;
			this.pageSetting.total = res.total;
			this.pageInfo.loading = false;
			this.shipStatService.shipImpBillStatTotal(params).subscribe((total: any) => {
				this.totalData = total;
			});
		});
	}

	stChange(ret: STChange) {
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.pageInfo.current = ret.pi;
			this.pageInfo.size = ret.ps;
			this.loadData(this.shipInfo.transportShipId);
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};

	changeShippingOrderStatus(item: any, status: string) {
		const postData = {
			id: item.id,
			shippingOrderStatus: status
		};
		this.shipStatService.updateShipBillStatus(postData).subscribe(res => {
			this.st.setRow(item, { shippingOrderStatus: res.shippingOrderStatus });
		});
	}

	changeIsFinish(item: any, isFinish: string) {
		const postData = {
			id: item.id,
			isImpFinish: isFinish
		};
		this.shipStatService.updateShipBillStatus(postData).subscribe(res => {
			this.st.setRow(item, { isImpFinish: res.isImpFinish });
		});
	}

	edit(item: any) {
		this.modalService
			.createDrag({
				nzContent: EditAllPieceTonComponent,
				nzComponentParams: {record: item},
				nzTitle: item?.billNo,
				nzFooter: null
			})
			.afterClose.subscribe(res => {
			this.loadData(this.shipInfo.transportShipId);
		});
	}

	clear(item: any) {
		this.shipStatService.clearStorage(item.transportBillId).subscribe(res => {
			if (res) {
				this.loadData(this.shipInfo.transportShipId);
			}
		});
	}

	billDetail(item: any) {
		item.shipNameZh = this.shipInfo.shipNameZh;
		item.voyage = this.shipInfo.impVoyage;
		item.ie = 'I';
		this.drawerService
			.create({
				nzContent: BillDetailComponent,
				nzContentParams: { record: item },
				nzWidth: 'calc(100vw - 200px)',
				nzBodyStyle: {
					background: '#f5f5f7',
				},
			})
			.afterClose.subscribe((res: any) => {});
	}

	export(shipId: string) {
		const params = {
			shipNameZh: this.shipInfo.shipNameZh,
			impVoyage: this.shipInfo.impVoyage,
			transportShipId: shipId,
			...this.searchForm.value,
		};
		window.open(`${environment.FINE_REPORT_URL}handle-tally/jinkouchuantongji.cpt&__parameters__=${JSON.stringify(params)}`, '_blank');
	}
}
